package com.wd.buycar.view.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.wd.buycar.R
import com.wd.buycar.view.activity.NewCarClooseCarActivity
import com.wd.common.bean.BuyCarBean.BuyCarListBean


/**
 * 作者：马海钊
 * 时间：2023/9/11 17:23
 * 功能：
 */
class BuynewcarVAdapterList(var context: Context, var list: List<BuyCarListBean.Result>) :
    RecyclerView.Adapter<BuynewcarVAdapterList.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view =
            LayoutInflater.from(context).inflate(R.layout.query_newcar_list1_small, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.name.text = list[position].brandList.get(0).name
        Glide.with(context).load(list[position].brandList.get(0).imageUrl)
            .error(R.mipmap.buy_oldcar_back1)
            .placeholder(R.mipmap.buy_oldcar_back1)
            .into(holder.imgid)
        holder.node.setText(list.get(position).node)
        holder.itemView.setOnClickListener {
            val intent = Intent(context, NewCarClooseCarActivity::class.java)
            intent.putExtra("id", list.get(position).brandList.get(0).brandId)
//            val sp = context.getSharedPreferences("user_car", MODE_PRIVATE)
//            val edit = sp.edit()
//
//            val valueList = listOf(list.get(position).brandList.get(0).name) // 要存储的 List 数据
//            val json = Gson().toJson(valueList)
//
//
//            edit.putString("name",list.get(position).brandList.get(0).name)
//
//            edit.apply()


            context.startActivity(intent)

        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var name: TextView
        var node: TextView
        var imgid: ImageView

        init {
            name = itemView.findViewById(R.id.query_newcar_list1_small_name)
            node = itemView.findViewById(R.id.query_newcar_list1_small_node)
            imgid = itemView.findViewById(R.id.query_newcar_list1_small_image)
        }
    }
}